package com.cc.service.impl;

import com.cc.mapper.EmpExprMapper;
import com.cc.mapper.EmpMapper;
import com.cc.pojo.*;
import com.cc.service.EmpService;

import com.cc.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> page(EmpQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        List<Emp> empList=empMapper.list(param);
        Page<Emp> empPage=(Page<Emp>) empList;
        return new PageResult(empPage.getTotal(),empPage.getResult());

    }

    @Override
    public void delect(List<Integer> ids) {
        empMapper.delect(ids);
        empExprMapper.delect(ids);
    }
    @Transactional
    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.add(emp);
        Integer id=emp.getId();
        List<EmpExpr> exprList=emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(id));
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public Emp getID(Integer id) {

        Emp emp=empMapper.getID(id);
        List<EmpExpr>empExprList=empExprMapper.findById(id);
        emp.setExprList(empExprList);
        return emp;
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        //删除旧工作经历
        empExprMapper.delect(Arrays.asList(emp.getId()));
        empMapper.update(emp);
        Integer empId=emp.getId();
        List<EmpExpr> exprList=emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insert(exprList);
        }

    }

    @Override
    public LoginInfo login(Emp emp) {

        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        if(empLogin != null){
            //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }
        return null;
    }

    @Override
    public List<Emp> findAll() {
        return empMapper.findAll();

    }

}
